package com.bdk.middle.management.web;


import com.bdk.middle.common.domain.PageDO;
import com.bdk.middle.common.status.ErrorCode;
import com.bdk.middle.management.api.domain.AdminDO;
import com.bdk.middle.management.api.domain.AdminDO2;
import com.bdk.middle.management.api.query.AdminQuery2DO;
import com.bdk.middle.management.api.query.AdminQueryDO;
import com.bdk.middle.management.api.query.SysUserQueryDO;
import com.bdk.middle.management.service.AdminService;
import com.bdk.middle.common.result.ResultDO;
import com.bdk.middle.common.result.web.ObjectResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;


/**
 * <p>
 * 用户登录表 前端控制器
 * </p>
 *
 * @author Reagan
 * @since 2021-01-23
 */
@RestController
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    private AdminService adminService;

    @PreAuthorize("hasRole('ROLE_superadmin')")
    @RequestMapping(value="/register.html", method = RequestMethod.POST)
    public @ResponseBody ObjectResponse register(@RequestBody AdminDO domain) {

        ObjectResponse response = new ObjectResponse();

        ResultDO result = adminService.add(domain);

        if(!result.isSuccess()) {
            response.setStatus(result.getErrorCode().getCode());
            response.setMsg(result.getErrorCode().getMsg());
        }
        return response;
    }

    @PreAuthorize("hasRole('ROLE_superadmin')")
    @RequestMapping(value="/edit.html", method = RequestMethod.POST)
    public @ResponseBody ObjectResponse edit(@RequestBody AdminQuery2DO domain) {

        ObjectResponse response = new ObjectResponse();

        ResultDO result = adminService.update(domain);

        if(!result.isSuccess()) {
            response.setStatus(result.getErrorCode().getCode());
            response.setMsg(result.getErrorCode().getMsg());
        }
        return response;
    }

    @PreAuthorize("hasRole('ROLE_superadmin')")
    @RequestMapping(value="/edit_password.html", method = RequestMethod.POST)
    public @ResponseBody ObjectResponse editPassword(@RequestBody AdminDO domain) {

        ObjectResponse response = new ObjectResponse();

        ResultDO result = adminService.editPassword(domain);

        if(!result.isSuccess()) {
            response.setStatus(result.getErrorCode().getCode());
            response.setMsg(result.getErrorCode().getMsg());
        }
        return response;
    }

    @PreAuthorize("hasRole('ROLE_superadmin')")
    @RequestMapping(value="/delete.html", method = RequestMethod.POST)
    public @ResponseBody ObjectResponse delete(@RequestBody AdminDO domain) {

        ObjectResponse response = new ObjectResponse();

        ResultDO result = adminService.remove(domain.getId(), null);

        if(!result.isSuccess()) {
            response.setStatus(result.getErrorCode().getCode());
            response.setMsg(result.getErrorCode().getMsg());
        }
        return response;
    }


//    @PreAuthorize("hasRole('ROLE_superadmin')")
//    @RequestMapping(value="/detail.html", method = RequestMethod.POST)
//    public @ResponseBody ObjectResponse detail(@RequestBody AdminDO domain) {
//
//        ObjectResponse<AdminDO> response = new ObjectResponse();
//
//        ResultDO result = adminService.get(domain.getId());
//
//        if(!result.isSuccess()) {
//            response.setStatus(result.getErrorCode().getCode());
//            response.setMsg(result.getErrorCode().getMsg());
//        } else {
//            response.setData((AdminDO) result.getModel(ResultDO.FIRST_MODEL_KEY));
//        }
//        return response;
//    }

    /**
     * 查询用户详情
     * @param domain
     * @return
     */
    @PreAuthorize("hasRole('ROLE_superadmin')")
    @RequestMapping(value="/detail.html", method = RequestMethod.POST)
    @ResponseBody
    public ObjectResponse detail(@RequestBody SysUserQueryDO domain) {

        ObjectResponse<AdminDO2> response = new ObjectResponse();
        if(domain.getUserId()==null || StringUtils.isEmpty(domain.getUserId())){
            response.setStatus(ErrorCode.REQUEST_PARAM_ERROR.getCode());
            response.setMsg(ErrorCode.REQUEST_PARAM_ERROR.getMsg());
        }
        ResultDO result = adminService.getDetail(domain.getUserId());

        if(!result.isSuccess()) {
            response.setStatus(result.getErrorCode().getCode());
            response.setMsg(result.getErrorCode().getMsg());
        } else {
            response.setData((AdminDO2) result.getModel(ResultDO.FIRST_MODEL_KEY));
        }
        return response;
    }


//    @PreAuthorize("hasRole('ROLE_superadmin')")
//    @RequestMapping(value="/list2.html", method = RequestMethod.POST)
//    public @ResponseBody ObjectResponse list(@RequestBody AdminQueryDO domain) {
//
//        ObjectResponse<PageDO<AdminDO>> response = new ObjectResponse();
//
//        ResultDO result = adminService.find(domain);
//
//        if(!result.isSuccess()) {
//            response.setStatus(result.getErrorCode().getCode());
//            response.setMsg(result.getErrorCode().getMsg());
//        } else {
//            response.setData((PageDO<AdminDO>) result.getModel(ResultDO.FIRST_MODEL_KEY));
//        }
//        return response;
//    }

    /**
     * 查询用户列表
     * @param domain
     * @return
     */
    @PreAuthorize("hasRole('ROLE_superadmin')")
    @RequestMapping(value="/list.html", method = RequestMethod.POST)
    public @ResponseBody ObjectResponse list(@RequestBody AdminQueryDO domain) {

        ObjectResponse<PageDO<AdminDO2>> response = new ObjectResponse();

        ResultDO result = adminService.find2(domain);

        if(!result.isSuccess()) {
            response.setStatus(result.getErrorCode().getCode());
            response.setMsg(result.getErrorCode().getMsg());
        } else {
            response.setData((PageDO<AdminDO2>) result.getModel(ResultDO.FIRST_MODEL_KEY));
        }
        return response;
    }

    /**
     * 查询用户是否存在
     * @param domain
     * @return
     */
    @PreAuthorize("hasRole('ROLE_superadmin')")
    @RequestMapping(value="/isUserExist.html", method = RequestMethod.POST)
    @ResponseBody
    public  ObjectResponse isUserExist(@RequestBody AdminQueryDO domain) {

        ObjectResponse response = new ObjectResponse();
        //参数校验
        if(domain.getUsername()==null || domain.getUsername().length()<3){
            response.setStatus(ErrorCode.REQUEST_PARAM_ERROR.getCode());
            response.setMsg(ErrorCode.REQUEST_PARAM_ERROR.getMsg());
        }
        ResultDO result = adminService.isUserExist(domain.getUsername());

        if(!result.isSuccess()) {
            response.setStatus(result.getErrorCode().getCode());
            response.setMsg(result.getErrorCode().getMsg());
        } else {
            response.setMsg("用户名不存在");
        }
        return response;
    }

    /**
     * 模糊查询用户列表
     * @param domain
     * @return
     */
    @PreAuthorize("hasRole('ROLE_superadmin')")
    @RequestMapping(value="/listByLike.html", method = RequestMethod.POST)
    public @ResponseBody ObjectResponse listByLike(@RequestBody AdminQueryDO domain) {

        ObjectResponse<PageDO<AdminDO2>> response = new ObjectResponse();
        if(domain.getUsername()==null || StringUtils.isEmpty(domain.getUsername())){
            //没传用户名,全查
            domain.setUsername(null);
            ResultDO result = adminService.find2(domain);

            if(!result.isSuccess()) {
                response.setStatus(result.getErrorCode().getCode());
                response.setMsg(result.getErrorCode().getMsg());
            } else {
                response.setData((PageDO<AdminDO2>) result.getModel(ResultDO.FIRST_MODEL_KEY));
            }
            return response;
        }
        //传用户名,则模糊查
        ResultDO result = adminService.findByLike(domain);

        if(!result.isSuccess()) {
            response.setStatus(result.getErrorCode().getCode());
            response.setMsg(result.getErrorCode().getMsg());
        } else {
            response.setData((PageDO<AdminDO2>) result.getModel(ResultDO.FIRST_MODEL_KEY));
        }
        return response;
    }

}

